package com.only.common.util;

import reactor.util.annotation.NonNull;
import reactor.util.annotation.Nullable;

import java.io.Serializable;
import java.util.Objects;

/**
 * @author only
 * @date 2023/10/17
 * @description
 */
public class Tuple2<T1, T2> implements Serializable {
    private static final long serialVersionUID = 1L;

    @NonNull
    public final T1 _t1;

    @NonNull
    public final T2 _t2;

    public Tuple2(T1 t1, T2 t2) {
        this._t1 = Objects.requireNonNull(t1, "t1");
        this._t2 = Objects.requireNonNull(t2, "t2");
    }

    public static <T1, T2> Tuple2<T1, T2> of(T1 t1, T2 t2) {
        return new Tuple2<>(t1, t2);
    }

    public T1 _t1() {
        return _t1;
    }

    public T2 _t2() {
        return _t2;
    }

    @Override
    public boolean equals(@Nullable Object o) {
        if (this == o) {
            return true;
        }

        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        Tuple2<?, ?> tuple2 = (Tuple2<?, ?>) o;

        return _t1.equals(tuple2._t1) && _t2.equals(tuple2._t2);
    }

    @Override
    public int hashCode() {
        int result = size();
        result = 31 * result + _t1.hashCode();
        result = 31 * result + _t2.hashCode();
        return result;
    }

    private int size() {
        return 2;
    }
}